package text.t1_516;

public class PolymorphicDemo {
    public static void main(String[] args) {
        Animal anl[] = new Animal[3];
        anl[0] = new Dog("狗子", 3, "黑");
        anl[1] = new Chick("坤哥",1,"白");
        anl[2] = new Fish("鱼儿",1,"红");

        //遍历数组
        for(int i = 0; i < anl.length; i++){
            System.out.println("动物的名字是：" + anl[i].name);
            anl[i].eat();
            anl[i].drink();

            if (anl[i] instanceof Dog) { //用于判断一个对象是否是某个类的实例，或者是否实现了某个接口，确保向下转型
                ((Dog) anl[i]).lookHome();
            } else if (anl[i] instanceof Chick) {
                ((Chick) anl[i]).layEggs();
            }

            if (anl[i] instanceof Swim) {
                ((Swim) anl[i]).swim();
            }
        }
        //喂养动物
        Master m = new Master();
        m.feed(anl[0]);
        m.feed(anl[1]);
        m.feed(anl[2]);
    }
}
